home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Sprocket Framework DR2 / Sprocket Framework / ThreadContext.cp < prev    next >
Text File  |  1996-06-15  |  4KB  |  212 lines

  1. /*
  2.  
  3.     File:        AEThreads.cp
  4.     Project:    Sprocket Framework 1.1 (DR2), released 6/15/96
  5.     Contains:    Routines for installing threaded AppleEvent handlers
  6.     To Do:        To really support AppleScript™, we’ll need to do ALOT more work in here.
  7.  
  8.     Sprocket Major Contributors:
  9.     ----------------------------
  10.     Dave Falkenburg, producer of Sprocket 1.0
  11.     Bill Hayden,     producer of Sprocket 1.1
  12.     Steve Sisak,     producer of the upcoming Sprocket 2.0
  13.     
  14.     Pete Alexander        Steve Falkenburg    Randy Thelen
  15.     Eric Berdahl        Nitin Ganatra        Chris K. Thomas
  16.     Marshall Clow        Dave Hershey        Leonard Rosenthal
  17.     Tim Craycroft        Dave Mark            Dean Yu
  18.     David denBoer        Gary Powell
  19.     Cameron Esfahani    Jon Summers            Apple Computer, Inc.
  20.         
  21.     Comments, Additions, or Corrections:
  22.     ------------------------------------
  23.     Bill Hayden, Nikol Software <nikol@codewell.com>
  24.  
  25. */
  26.  
  27. #ifndef        _THREADCONTEXT_
  28. #include    "ThreadContext.h"
  29. #endif
  30.  
  31. #include "SprocketMacros.h"
  32.  
  33. #include <OSUtils.h>
  34.  
  35.  
  36.  
  37. /*****************************************************************************/
  38.  
  39.  
  40.  
  41. TThreadContext::TThreadContext()
  42. {
  43.     fThreadID        = kNoThreadID;
  44.     fThreadResult    = nil;
  45.  
  46. #if !GENERATINGCFM
  47.     fSavedA5        = SetCurrentA5();
  48. #endif
  49.  
  50. #if CW7_EXCEPTIONS
  51.     // set up interface to C++ exception runtime support
  52.     __new_exception_state(&fExceptionState, fCatchBuffer, sizeof(fCatchBuffer));
  53. #endif
  54.     
  55. }
  56.  
  57.  
  58.  
  59. /*****************************************************************************/
  60.  
  61.  
  62.  
  63. TThreadContext::~TThreadContext()
  64. {
  65. }
  66.  
  67.  
  68.  
  69. /*****************************************************************************/
  70.  
  71.  
  72.  
  73. void TThreadContext::CreateThread(    ThreadStyle            threadStyle,
  74.                                     ThreadEntryProcPtr    threadEntry,
  75.                                     Size                stackSize,
  76.                                     ThreadOptions        options)
  77. {
  78.     FailOSErr(NewThread(threadStyle, threadEntry, this, stackSize, options, &fThreadResult, &fThreadID));
  79.  
  80.     try
  81.         {
  82. #if GENERATING68K && GENERATINGCFM
  83.         FailOSErr(SetThreadSwitcher(fThreadID, NewThreadSwitchProc(SwitchInProc), this, true));
  84.         FailOSErr(SetThreadSwitcher(fThreadID, NewThreadSwitchProc(SwitchOutProc), this, false));
  85.         FailOSErr(SetThreadTerminator(fThreadID, NewThreadTerminationProc(TerminationProc), this));
  86. #else
  87.         FailOSErr(SetThreadSwitcher(fThreadID, SwitchInProc, this, true));
  88.         FailOSErr(SetThreadSwitcher(fThreadID, SwitchOutProc, this, false));
  89.         FailOSErr(SetThreadTerminator(fThreadID, TerminationProc, this));
  90. #endif
  91.         }
  92.     catch(OSErr err)
  93.         {
  94.         OSErr ignore = DisposeThread(fThreadID, &fThreadResult, (options & kUsePremadeThread) != 0);
  95.  
  96.         fThreadID = kNoThreadID;
  97.         
  98.         throw(err);
  99.         }
  100. }
  101.  
  102.  
  103.  
  104. /*****************************************************************************/
  105.  
  106.  
  107.  
  108. void TThreadContext::SwitchIn(void)
  109. {
  110. #if CW7_EXCEPTIONS
  111.     // restore exception-handling context
  112.     ExceptionState dummy;
  113.     __switch_exception_state(&fExceptionState, &dummy);
  114. #endif
  115. }
  116.  
  117.  
  118.  
  119. /*****************************************************************************/
  120.  
  121.  
  122.  
  123. void TThreadContext::SwitchOut(void)
  124. {
  125. #if CW7_EXCEPTIONS
  126.     // save exception-handling context
  127.     __switch_exception_state(&fExceptionState, &fExceptionState);
  128. #endif
  129. }
  130.  
  131.  
  132.  
  133. /*****************************************************************************/
  134.  
  135.  
  136.  
  137. void TThreadContext::Terminate(void)
  138. {
  139.     if (fThreadID != kNoThreadID)
  140.     {
  141.         fThreadID = kNoThreadID;
  142.     }
  143. }    
  144.  
  145.  
  146.  
  147. /*****************************************************************************/
  148.  
  149.  
  150.  
  151. pascal void TThreadContext::SwitchInProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
  152. {
  153.     TThreadContext*    thread = (TThreadContext*) switchProcParam;
  154.     
  155. #if !GENERATINGCFM
  156.     long savedA5 = SetA5(thread->fSavedA5);
  157. #endif
  158.  
  159. #if THREAD_PROFILE
  160.     ::ProfilerSwitchToThread(thread->mProfilerRef);
  161. #endif
  162.     
  163.     thread->SwitchIn();
  164.     
  165. #if !GENERATINGCFM
  166.     SetA5(savedA5);
  167. #endif
  168. }
  169.  
  170.  
  171.  
  172. /*****************************************************************************/
  173.  
  174.  
  175.  
  176. pascal void TThreadContext::SwitchOutProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
  177. {
  178.     TThreadContext*    thread = (TThreadContext*) switchProcParam;
  179.     
  180. #if !GENERATINGCFM
  181.     long savedA5 = SetA5(thread->fSavedA5);
  182. #endif
  183.      thread->SwitchOut();
  184.  
  185. #if !GENERATINGCFM
  186.     SetA5(savedA5);
  187. #endif
  188. }
  189.  
  190.  
  191.  
  192. /*****************************************************************************/
  193.  
  194.  
  195.  
  196. pascal void TThreadContext::TerminationProc(ThreadID /*threadBeingSwitched*/, void *terminationProcParam)
  197. {
  198.     TThreadContext*    thread = (TThreadContext*) terminationProcParam;
  199.     
  200. #if !GENERATINGCFM
  201.     long savedA5 = SetA5(thread->fSavedA5);
  202. #endif
  203.  
  204.     thread->Terminate();
  205.  
  206. #if !GENERATINGCFM
  207.     SetA5(savedA5);
  208. #endif
  209. }
  210.  
  211.  
  212.